﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;
using l1cs.server.templates;

namespace l1cs.server.clientpackets
{
    public class C_Shop : ClientBasePacket
    {
        private const String _C_SHOP = "[C] C_Shop";
        private static Logger _log = Logger.getLogger( typeof( C_Shop ).FullName );

        public C_Shop( byte[] abyte0, ClientThread clientthread )
            : base( abyte0 )
        {

            L1PcInstance pc = clientthread.getActiveChar();
            if ( pc.isGhost() )
            {
                return;
            }

            int mapId = pc.getMapId();
            if ( mapId != 340 && mapId != 350 && mapId != 360 && mapId != 370 )
            {
                pc.sendPackets( new S_ServerMessage( 876 ) ); // この場所では個人商店を開けません。
                return;
            }

            List<L1PrivateShopSellList> sellList = pc.getSellList();
            List<L1PrivateShopBuyList> buyList = pc.getBuyList();
            L1ItemInstance checkItem;
            bool tradable = true;

            int type = readC();
            if ( type == 0 )
            { // 開始
                int sellTotalCount = readH();
                int sellObjectId;
                int sellPrice;
                int sellCount;
                for ( int i = 0; i < sellTotalCount; i++ )
                {
                    sellObjectId = readD();
                    sellPrice = readD();
                    sellCount = readD();
                    // 取引可能なアイテムかチェック
                    checkItem = pc.getInventory().getItem( sellObjectId );
                    if ( !checkItem.getItem().isTradable() )
                    {
                        tradable = false;
                        pc.sendPackets( new S_ServerMessage( 166, // \f1%0が%4%1%3%2
                                checkItem.getItem().getName(), "取引不可能です。" ) );
                    }
                    //Object[] petlist = pc.getPetList().values().toArray();
                    foreach ( Object petObject in pc.getPetList().Values )
                    {
                        if ( petObject is L1PetInstance )
                        {
                            L1PetInstance pet = (L1PetInstance)petObject;
                            if ( checkItem.getId() == pet.getItemObjId() )
                            {
                                tradable = false;
                                pc.sendPackets( new S_ServerMessage( 166, // \f1%0が%4%1%3%2
                                        checkItem.getItem().getName(),
                                        "取引不可能です。" ) );
                                break;
                            }
                        }
                    }
                    L1PrivateShopSellList pssl = new L1PrivateShopSellList();
                    pssl.setItemObjectId( sellObjectId );
                    pssl.setSellPrice( sellPrice );
                    pssl.setSellTotalCount( sellCount );
                    sellList.Add( pssl );
                }
                int buyTotalCount = readH();
                int buyObjectId;
                int buyPrice;
                int buyCount;
                for ( int i = 0; i < buyTotalCount; i++ )
                {
                    buyObjectId = readD();
                    buyPrice = readD();
                    buyCount = readD();
                    // 取引可能なアイテムかチェック
                    checkItem = pc.getInventory().getItem( buyObjectId );
                    if ( !checkItem.getItem().isTradable() )
                    {
                        tradable = false;
                        pc.sendPackets( new S_ServerMessage( 166, // \f1%0が%4%1%3%2
                                checkItem.getItem().getName(), "取引不可能です。" ) );
                    }
                    if ( checkItem.getBless() >= 128 )
                    { // 封印された装備
                        // \f1%0は捨てたりまたは他人に讓ることができません。
                        pc.sendPackets( new S_ServerMessage( 210, checkItem.getItem()
                                .getName() ) );
                        return;
                    }

                    //Object[] petlist = pc.getPetList().values().toArray();
                    foreach ( Object petObject in pc.getPetList().Values )
                    {
                        if ( petObject is L1PetInstance )
                        {
                            L1PetInstance pet = (L1PetInstance)petObject;
                            if ( checkItem.getId() == pet.getItemObjId() )
                            {
                                tradable = false;
                                pc.sendPackets( new S_ServerMessage( 166, // \f1%0が%4%1%3%2
                                        checkItem.getItem().getName(),
                                        "取引不可能です。" ) );
                                break;
                            }
                        }
                    }
                    L1PrivateShopBuyList psbl = new L1PrivateShopBuyList();
                    psbl.setItemObjectId( buyObjectId );
                    psbl.setBuyPrice( buyPrice );
                    psbl.setBuyTotalCount( buyCount );
                    buyList.Add( psbl );
                }
                if ( !tradable )
                { // 取引不可能なアイテムが含まれている場合、個人商店終了
                    sellList.Clear();
                    buyList.Clear();
                    pc.setPrivateShop( false );
                    pc.sendPackets( new S_DoActionGFX( pc.getId(),
                            ActionCodes.ACTION_Idle ) );
                    pc.broadcastPacket( new S_DoActionGFX( pc.getId(),
                            ActionCodes.ACTION_Idle ) );
                    return;
                }
                byte[] chat = readByte();
                pc.setShopChat( chat );
                pc.setPrivateShop( true );
                pc.sendPackets( new S_DoActionShop( pc.getId(),
                        ActionCodes.ACTION_Shop, chat ) );
                pc.broadcastPacket( new S_DoActionShop( pc.getId(),
                        ActionCodes.ACTION_Shop, chat ) );
            }
            else if ( type == 1 )
            { // 終了
                sellList.Clear();
                buyList.Clear();
                pc.setPrivateShop( false );
                pc.sendPackets( new S_DoActionGFX( pc.getId(),
                        ActionCodes.ACTION_Idle ) );
                pc.broadcastPacket( new S_DoActionGFX( pc.getId(),
                        ActionCodes.ACTION_Idle ) );
            }
        }

        override
        public String getType()
        {
            return _C_SHOP;
        }
    }
}